const App = getApp();
const navigationBarHeight = (getApp().diygwstatusBarHeight + 44) + 'px'
Page({
    data: {
       total:0,
        quan:0,
        msgs1: [],
        url: App.Config.basePath,
        users: []
    },

    showModal(message) {
        App.WxService.showModal({
            title: '友情提示',
            content: message,
            showCancel: !1,
        });
    },
    onShareAppMessage: function() {},
    onLoad(option) {
        if (option) {
            this.setData({
                globalOption: option,
                quan:option.quan,
                total:option.total,
                msgs1:wx.getStorageSync("m"),
            })
        }


        var mid= wx.getStorageSync("uid");

        if (mid=="") {
            this.denglu();
        } else {
            this.getForm1();
            this.chushi();
        }


    },
    onShow() {

    },

    chushi() {
        //调用数据
        let param = {
            f: 1,
            id: wx.getStorageSync("uid"),
            loadmsg: `正在加载中`
        };


        App.HttpService.getData(param, "/users_List").then(data => {

            this.setData({

                users: data
            });
        });
    },

    onChangeAddress: function () {
        var _page = this;
        wx.chooseLocation({
            success: function (res) {
                console.log(res);
                _page.setData({
                    address: res.address
                });
            },
            fail: function (err) {
                console.log(err)
            }
        });
    },

    //设置输入验证
    getForm1() {
        this.WxValidate = App.WxValidate({
            username: {
                required:{
                    message:'姓名：不能为空哟'
                }
            },
            phone: {
                tel:{
                    message:'手机号码：请输入11位的手机号码'
                },
                required:{
                    message:'手机号码：不能为空哟'
                }
            },

            address: {
                required:{
                    message:'收货地址：不能为空哟'
                }
            },

        })
    },
    formSubmit1: function(e) {
        if (!this.WxValidate.checkForm(e)) {
            const error = this.WxValidate.errorList[0]
            var msg = `${error.msg}`;
            this.showModal(msg);
            return false
        } else {



            var data = e.detail.value;

            data = App.Tools.extend(data, {
                f: 1,
                omoney:this.data.total,
                uid: wx.getStorageSync("uid"),
                cartItems:JSON.stringify(this.data.msgs1),
            });

            App.HttpService.saveData(data, "/orders_Add").then(data => {
                App.WxService.showToast({
                    title: "提交成功!",
                    icon: 'success',
                    duration: 1500,
                });
                setTimeout(function() {
                    wx.switchTab({
                        url:"/pages/me/index"
                    })
                }, 1500)
            });
        }
    },
    formReset1: function() {
        console.log('form发生了reset事件')
    },


    denglu(e)
    {
        this.setData({
            modalName:"Modal"
        })



    },

    login(e)
    {
        var that=this;
        wx.getUserProfile({
            desc:'获取用户相关信息',
            success: res => {
                App.globalData.userInfo = res.userInfo
                console.log(App.globalData.userInfo);

                //调用数据
                let param = {
                    mname:App.globalData.userInfo.nickName,
                    addr:App.globalData.userInfo.avatarUrl
                };

                //调用数据
                App.HttpService.getData(param,"/users_Add").then(data=>{

                    wx.setStorageSync("uid",data[0].jieguo);

                    App.WxService.showToast({
                        title: "登录成功!",
                        icon: 'success',
                        duration: 1500,
                    });
                    setTimeout(function() {

                    }, 1500)

                    that.chushi();
                    this.setData({
                        mid:-1,
                        modalName: null
                    })
                });


            }
        })
    },


})
